package com.quanyan.place.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.enums.SysParameterConfigEnum;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.DateTimeUtils;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.place.biz.IBaseService;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.*;
import com.quanyan.place.entity.apireq.ReqOrderHistory;
import com.quanyan.place.entity.apiresp.RespBarCodeInfo;
import com.quanyan.place.entity.apiresp.RespPlaceGoodsCardInfo;
import com.quanyan.place.entity.apiresp.RespPlaceGoodsTicketInfo;
import com.quanyan.place.entity.apiresp.RespPlaceGoodsUnitInfo;
import com.quanyan.place.entity.apiresp.RespPlacePrintInfo;
import com.quanyan.place.entity.vo.PlaceFinanceAuditVo;
import com.quanyan.place.entity.vo.PlacePrintVo;
import com.quanyan.place.mapper.*;
import com.quanyan.place.service.BaseService;
import com.quanyan.place.service.PlaceBaseService;
import com.quanyan.place.service.PlaceOrderUtilService;
import com.quanyan.place.wrapper.impl.PlaceOrderServiceWrapperImpl;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections.comparators.ComparatorChain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import sms.SmsApi;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by youxiaojia on 2017/2/9.
 */
@Service
public class PlaceOrderUtilServiceImpl implements PlaceOrderUtilService {
    private static final Logger logger = LoggerFactory.getLogger(PlaceOrderServiceWrapperImpl.class);

    @Autowired
    TbBizPlaceOrderHistoryMapper tbBizPlaceOrderHistoryMapper;
    @Autowired
    VPlaceMapper vPlaceMapper;
    @Autowired
    IBaseService iBaseService;
    @Autowired
    TbBizPlaceAttachMapper tbBizPlaceAttachMapper;
    @Autowired
    TbAdminUserMapper tbAdminUserMapper;
    @Autowired
    TbBizPlacePrintMapper tbBizPlacePrintMapper;
    @Autowired
    TbBizPlaceOrderItemMapper tbBizPlaceOrderItemMapper;
    @Autowired
    TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;
    @Autowired
    TbPlaceTicketSalePlanMapper tbPlaceTicketSalePlanMapper;
    @Autowired
    TbBizPlaceGoodsMapper tbBizPlaceGoodsMapper;
    @Autowired
    TbBizPlaceMapper tbBizPlaceMapper;
    @Autowired
    TbBizPlaceOrderMapper tbBizPlaceOrderMapper;
    @Autowired
    TbBizPlacePayItemMapper tbBizPlacePayItemMapper;
    @Autowired
    TbBizPlaceMemberOpHistoryMapper tbBizPlaceMemberOpHistoryMapper;
    @Autowired
    TbBizPlacePrivateOrderMapper tbBizPlacePrivateOrderMapper;
    @Autowired
    TbBizPlaceMemberCardMapper tbBizPlaceMemberCardMapper;
    @Autowired
    TbBizPlaceMemberCardTypeMapper tbBizPlaceMemberCardTypeMapper;
    @Autowired
    DictMapper dictMapper;
    @Autowired
    PosUnionPayMapper posUnionPayMapper;
    @Autowired
    PlaceBaseService placeBaseService;
    @Autowired
    BaseService baseService;
    @Autowired
    com.quanyan.place.biz.SmsService  placeSmsService;

    /** 插入支付退款订单流水 **/
    @Override
    public void insertOrderHistory(ReqOrderHistory reqOrderHistory) throws Exception{
        TbBizPlaceOrderHistory tbBizPlaceOrderHistory = new TbBizPlaceOrderHistory();
        BeanUtils.copyProperties(tbBizPlaceOrderHistory,reqOrderHistory);
        tbBizPlaceOrderHistoryMapper.insertSelective(tbBizPlaceOrderHistory);
    }

    @Override
    public void shoppingCartOrderInsertOrderHistory(List<ReqOrderHistory> reqOrderHistoryList) throws Exception {
        for (ReqOrderHistory reqOrderHistory : reqOrderHistoryList){
            TbBizPlaceOrderHistory tbBizPlaceOrderHistory = new TbBizPlaceOrderHistory();
            BeanUtils.copyProperties(tbBizPlaceOrderHistory,reqOrderHistory);
            tbBizPlaceOrderHistoryMapper.insertSelective(tbBizPlaceOrderHistory);
        }
        //对购物车会员订单发送短信
        shoppingCartOrderSms(reqOrderHistoryList);
    }

    /** 订单转换订单流水
     * @param tbBizPlaceOrder
     * @param receiveAmount 实收金额 (排除 鸟蛋 优惠券 调价 会员优惠 等优惠方式)
     * @param businessType 交易类型：0：支付 1：退款
     * @param refundType 0：全额 1：部分 退款
     * @return
     */
    @Override
    public ReqOrderHistory tbBizPlaceOrder2ReqOrderHistory(TbBizPlaceOrder tbBizPlaceOrder,
                                                           BigDecimal receiveAmount,
                                                           byte businessType,
                                                           byte refundType) {
        if (tbBizPlaceOrder == null){
            return null;
        }
        ReqOrderHistory reqOrderHistory = new ReqOrderHistory();

        reqOrderHistory.setPlaceId(tbBizPlaceOrder.getPlaceId());
        reqOrderHistory.setAmount(receiveAmount);
        reqOrderHistory.setBusinessType(businessType);
        reqOrderHistory.setParentId(tbBizPlaceOrder.getParentId());
        reqOrderHistory.setPlatform(tbBizPlaceOrder.getPlatform());
        reqOrderHistory.setRefundType(refundType);
        reqOrderHistory.setSourceType(tbBizPlaceOrder.getSourceType());
        reqOrderHistory.setUid(tbBizPlaceOrder.getUid());
        reqOrderHistory.setUseType(tbBizPlaceOrder.getUseType());
        reqOrderHistory.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
        return reqOrderHistory;
    }

    /** 次卡门票订单转换订单流水
     * @param tbBizPlaceOrder
     * @param ticketCount 扣除次数
     * @param businessType 交易类型：0：支付 1：退款
     * @param refundType 0：全额 1：部分 退款
     * @return
     */
    @Override
    public ReqOrderHistory memberTicket2ReqOrderHistory(TbBizPlaceOrder tbBizPlaceOrder,
                                                           int ticketCount,
                                                           byte businessType,
                                                           byte refundType) {
        if (tbBizPlaceOrder == null){
            return null;
        }
        ReqOrderHistory reqOrderHistory = new ReqOrderHistory();

        reqOrderHistory.setPlaceId(tbBizPlaceOrder.getPlaceId());
        reqOrderHistory.setTicketCount(ticketCount);
        reqOrderHistory.setBusinessType(businessType);
        reqOrderHistory.setParentId(tbBizPlaceOrder.getParentId());
        reqOrderHistory.setPlatform(tbBizPlaceOrder.getPlatform());
        reqOrderHistory.setRefundType(refundType);
        reqOrderHistory.setSourceType(tbBizPlaceOrder.getSourceType());
        reqOrderHistory.setUid(tbBizPlaceOrder.getUid());
        reqOrderHistory.setUseType(tbBizPlaceOrder.getUseType());
        reqOrderHistory.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
        return reqOrderHistory;
    }

    /**
     * 修改场馆订单运营方式
     * @param placeId
     * @param businessMode
     */
    @Override
    public void updatePlaceOrderBusinessMode(Integer placeId, Byte businessMode) {
        if (placeId == null || businessMode == null){
            return;
        }
        vPlaceMapper.updatePlaceOrderBusinessMode(placeId,businessMode);
    }

    
    
    @Override
    public RespPlacePrintInfo getPrintOrderInfo(PlacePrintVo placePrintVo) {
        /*private String placeName;//场馆名称
        private String businessTime;//营业时间
        private String bookPhone;//订场电话
        private String placeAddress;//场馆地址
        private String placeOrderId;//订单号
        private String placeOrderTime;//下单时间
        private String customerName;//客户名称
        private String customerPhone;//客户电话
        private String memberCardNo;//会员卡号
        private String itemName;//消费项目
        private BigDecimal totalAmount;//费用合计
        private BigDecimal discountAmount;//优惠金额
        private String realAmount;//实付金额
        private String balanceAmount;//会员卡余额
        private String cashier;//收银员
        private String memo;//小票备注*/

        if (placePrintVo == null){
            return null;
        }

        TbBizPlaceOrder tbBizPlaceOrder = new TbBizPlaceOrder();
        TbBizPlace tbBizPlace = null;
        List<TbBizPlacePayItem> tbBizPlacePayItemAll = null;
        List<String> bookAllSonOrderNo = new ArrayList<>();

        //订单金额
        BigDecimal orderAmount = null;
        //会员卡名称
        String cardName = null;
        //会员卡号，
        String cardNo = null;
        //优惠金额
        BigDecimal discountAmount = null;
        //会员卡余额
        BigDecimal balanceAmount = null;
        //会员卡赠送金额
        BigDecimal giveAmount = null;
        //次卡实付次数
        Integer opNum = null;
        //次卡剩余次数
        Integer balanceNum = null;
        //次卡赠送次数
        Integer giveNum = null;
        //是否次卡
        boolean isNumCard = false;
        //是否 未付款
        boolean isNotPay = false;
        //实付金额
        BigDecimal realAmount = new BigDecimal(0);
        //手动调价
        BigDecimal handAdjustAmount = new BigDecimal(0);
        //业务类型
        Integer businessType = null;
        //支付方式id  （24,25,26,35,11,7不显示）
        String payType = null;
        //需要隐藏的支付方式
        List<String> hiddenPayTypeList =  Arrays.asList("7", "11", "24", "25","26","35","37");
        //优惠的支付方式
        List<String> discountPayTypeList = Arrays.asList("7", "11");
        //费用合计
        BigDecimal totalPrice = new BigDecimal(0);

        //全部支付方式
        Map<String,String> payTypeMap = new HashMap<>();
        DictExample dictExample = new DictExample();
        dictExample.createCriteria().andTagGroupIn(Arrays.asList("25", "26"));
        List<Dict> payTypeDictList = dictMapper.selectByExample(dictExample);
        if (CollectionUtils.isNotEmpty(payTypeDictList)){
            for (Dict dict : payTypeDictList){
                payTypeMap.put(dict.getDataKey(),dict.getDataValue());
            }
        }

        RespPlacePrintInfo respPlacePrintInfo = new RespPlacePrintInfo();

        //开单、门票、小商品、包场子订单、购物车
        if (PlaceConstants.ORDER_USE_BILLING == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_TICKET == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_GOODS == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_APP_BILLING == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_SHOPPING_CART == placePrintVo.getBusinessType()
                || (PlaceConstants.ORDER_USE_BLOCK_BOOKING == placePrintVo.getBusinessType()
                   && StringUtils.isEmpty(placePrintVo.getPlacePrivateOrderId()))){
            TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
            tbBizPlaceOrderExample.createCriteria().andPlaceOrderIdEqualTo(placePrintVo.getPlaceOrderId());
            List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
            if (CollectionUtils.isEmpty(tbBizPlaceOrderList)){
                try {
                    throw new Exception("不存在该订单");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            tbBizPlaceOrder = tbBizPlaceOrderList.get(0);
            tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(tbBizPlaceOrder.getPlaceId());
            TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
            tbBizPlacePayItemExample.createCriteria().andPlaceOrderIdEqualTo(placePrintVo.getPlaceOrderId());
            List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
            if (CollectionUtils.isNotEmpty(tbBizPlacePayItemList)){
                tbBizPlacePayItemAll = tbBizPlacePayItemList;
                for (TbBizPlacePayItem tbBizPlacePayItem :tbBizPlacePayItemAll){
                    //排除不需要显示的支付方式
                    if (tbBizPlacePayItem.getPayType() != null){
                        if (!discountPayTypeList.contains(tbBizPlacePayItem.getPayType().toString())){
                            realAmount = realAmount.add(tbBizPlacePayItem.getAmount());
                        }
                        if (!hiddenPayTypeList.contains(tbBizPlacePayItem.getAdminPayType().toString())){
                            payType = tbBizPlacePayItem.getAdminPayType().toString();
                        }
                        /*
                        //app 优惠券全部抵扣
                        if (discountPayTypeList.contains(tbBizPlacePayItem.getPayType().toString())){
                            if (tbBizPlacePayItem.getPayType().intValue() == PlaceConstants.PAY_WAY_FOR_AGES){//鸟蛋的优惠方式
                                discountAmount = (discountAmount == null ?new BigDecimal(0) : discountAmount).subtract(tbBizPlacePayItem.getAmount().divide(new BigDecimal(10)));
                            }else{
                                discountAmount = (discountAmount == null ?new BigDecimal(0) : discountAmount).subtract(tbBizPlacePayItem.getAmount());
                            }
                        }
                        */
                    }
                }
            } else {
                respPlacePrintInfo.setRealAmount("未付款");
                isNotPay = true;
            }
            handAdjustAmount = tbBizPlaceOrder.getHandAdjustAmount();
            orderAmount = tbBizPlaceOrder.getDebit();
            businessType = (int)placePrintVo.getBusinessType();
            respPlacePrintInfo.setOrderMemo(tbBizPlaceOrder.getDescrible());
        }
        //包场  -- 直接支付下单  
        if (PlaceConstants.ORDER_USE_BLOCK_BOOKING == placePrintVo.getBusinessType() && !StringUtils.isEmpty(placePrintVo.getPlacePrivateOrderId())){
            TbBizPlacePrivateOrderExample tbBizPlacePrivateOrderExample = new TbBizPlacePrivateOrderExample();
            tbBizPlacePrivateOrderExample.createCriteria().andPlaceOrderIdEqualTo(placePrintVo.getPlacePrivateOrderId());
            List<TbBizPlacePrivateOrder> tbBizPlacePrivateOrderList = tbBizPlacePrivateOrderMapper.selectByExample(tbBizPlacePrivateOrderExample);
            if (CollectionUtils.isEmpty(tbBizPlacePrivateOrderList)){
                try {
                    throw new Exception("不存在该包场订单");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                BeanUtils.copyProperties(tbBizPlaceOrder,tbBizPlacePrivateOrderList.get(0));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            tbBizPlaceOrder.setUseType(PlaceConstants.ORDER_USE_BLOCK_BOOKING);
            //查询所有包场子订单
            TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
            tbBizPlaceOrderExample.createCriteria().andParentIdEqualTo(tbBizPlacePrivateOrderList.get(0).getId());
            List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
            if (CollectionUtils.isEmpty(tbBizPlaceOrderList)){
                try {
                    throw new Exception("不存在该包场子订单");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            for (TbBizPlaceOrder tbBizPlaceOrder1 : tbBizPlaceOrderList){
                bookAllSonOrderNo.add(tbBizPlaceOrder1.getPlaceOrderId());
            }
            TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
            tbBizPlacePayItemExample.createCriteria().andPlaceOrderIdIn(bookAllSonOrderNo);
            tbBizPlacePayItemAll = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
            tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(tbBizPlaceOrder.getPlaceId());
            handAdjustAmount = tbBizPlaceOrder.getHandAdjustAmount();
            for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemAll){
                realAmount = realAmount.add(tbBizPlacePayItem.getAmount());
                //排除不需要显示的支付方式
                if (tbBizPlacePayItem.getPayType() != null) {
                    if (!hiddenPayTypeList.contains(tbBizPlacePayItem.getAdminPayType().toString())) {
                        payType = tbBizPlacePayItem.getAdminPayType().toString();
                    }
                }
            }
            orderAmount = tbBizPlaceOrder.getDebit();
            businessType = (int)placePrintVo.getBusinessType();
            respPlacePrintInfo.setOrderMemo(tbBizPlaceOrder.getDescrible());
        }
        //开卡充值
        if (PlaceConstants.ORDER_USE_OPEN_CARD == placePrintVo.getBusinessType() || PlaceConstants.ORDER_USE_RECHARGE_CARD == placePrintVo.getBusinessType()){
            TbBizPlaceMemberOpHistoryExample tbBizPlaceMemberOpHistoryExample = new TbBizPlaceMemberOpHistoryExample();
            tbBizPlaceMemberOpHistoryExample.createCriteria().andOrderNoEqualTo(placePrintVo.getPlaceOrderId());
            List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList = tbBizPlaceMemberOpHistoryMapper.selectByExample(tbBizPlaceMemberOpHistoryExample);
            if (CollectionUtils.isEmpty(tbBizPlaceMemberOpHistoryList)){
                try {
                    throw new Exception("不存在该开卡/充值记录");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            tbBizPlaceOrder.setPlaceId(tbBizPlaceMemberOpHistoryList.get(0).getPlaceId());
            tbBizPlaceOrder.setPlaceOrderId(tbBizPlaceMemberOpHistoryList.get(0).getOrderNo());
            tbBizPlaceOrder.setCustomerName(tbBizPlaceMemberOpHistoryList.get(0).getUserName());
            tbBizPlaceOrder.setMobile(tbBizPlaceMemberOpHistoryList.get(0).getMobile());
            tbBizPlaceOrder.setDescrible(tbBizPlaceMemberOpHistoryList.get(0).getCardTypeName());
            tbBizPlaceOrder.setDebit(tbBizPlaceMemberOpHistoryList.get(0).getAmount());
            tbBizPlaceOrder.setCreateUid(tbBizPlaceMemberOpHistoryList.get(0).getCreateUid());
            tbBizPlaceOrder.setSourceType(tbBizPlaceMemberOpHistoryList.get(0).getSourceType());
            tbBizPlaceOrder.setOrderTime(tbBizPlaceMemberOpHistoryList.get(0).getCreateTime());
            tbBizPlaceOrder.setDescrible(tbBizPlaceMemberOpHistoryList.get(0).getMemo());
            tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(tbBizPlaceOrder.getPlaceId());

            realAmount = tbBizPlaceMemberOpHistoryList.get(0).getAmount();
            //排除不需要显示的支付方式
            if (tbBizPlaceMemberOpHistoryList.get(0).getAdminPayType() != null) {
                if (!hiddenPayTypeList.contains(tbBizPlaceMemberOpHistoryList.get(0).getAdminPayType().toString())) {
                    payType = tbBizPlaceMemberOpHistoryList.get(0).getAdminPayType().toString();
                }
            }
            orderAmount = tbBizPlaceOrder.getDebit();
            balanceAmount = tbBizPlaceMemberOpHistoryList.get(0).getOpAfterAmount().add(tbBizPlaceMemberOpHistoryList.get(0).getOpAfterGiveAmount());
            giveAmount = tbBizPlaceMemberOpHistoryList.get(0).getOpAfterGiveAmount().subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeGiveAmount());
            opNum = tbBizPlaceMemberOpHistoryList.get(0).getOpCardCount();
            balanceNum = tbBizPlaceMemberOpHistoryList.get(0).getOpAfterNumbers();

            Integer cardTypeId = null;
            if (tbBizPlaceMemberOpHistoryList.get(0).getCardTypeId() != null){
                cardTypeId = tbBizPlaceMemberOpHistoryList.get(0).getCardTypeId();
            } else {
                TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
                tbBizPlaceMemberCardExample.createCriteria().andInnerCardNoEqualTo(tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo());
                List<TbBizPlaceMemberCard> tbBizPlaceMemberCardList = tbBizPlaceMemberCardMapper.selectByExample(tbBizPlaceMemberCardExample);
                if (CollectionUtils.isEmpty(tbBizPlaceMemberCardList)){
                    try {
                        throw new Exception("根据历史记录找不到会员卡");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                cardTypeId = tbBizPlaceMemberCardList.get(0).getCardTypeId();
            }
            if (cardTypeId != null){
                TbBizPlaceMemberCardTypeExample tbBizPlaceMemberCardTypeExample = new TbBizPlaceMemberCardTypeExample();
                tbBizPlaceMemberCardTypeExample.createCriteria().andIdEqualTo(cardTypeId);
                List<TbBizPlaceMemberCardType> tbBizPlaceMemberCardTypeList = tbBizPlaceMemberCardTypeMapper.selectByExample(tbBizPlaceMemberCardTypeExample);
                if (CollectionUtils.isEmpty(tbBizPlaceMemberCardTypeList)){
                    try {
                        throw new Exception("找不到会员卡类型");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                cardName = tbBizPlaceMemberCardTypeList.get(0).getName();
                giveNum = tbBizPlaceMemberCardTypeList.get(0).getFrequency();
            }
            businessType = (int)placePrintVo.getBusinessType();
            respPlacePrintInfo.setOrderMemo(tbBizPlaceOrder.getDescrible());
        }

        TbBizPlaceAttachExample tbBizPlaceAttachExample = new TbBizPlaceAttachExample();
        tbBizPlaceAttachExample.createCriteria().andPlaceIdEqualTo(tbBizPlaceOrder.getPlaceId());
        List<TbBizPlaceAttach> placeAttachList = tbBizPlaceAttachMapper.selectByExample(tbBizPlaceAttachExample);


        respPlacePrintInfo.setPlaceName(tbBizPlace.getName());
        if (CollectionUtils.isNotEmpty(placeAttachList)){
            respPlacePrintInfo.setBusinessTime(placeAttachList.get(0).getBusinessHourStart()+"-"+placeAttachList.get(0).getBusinessHourEnd());
            respPlacePrintInfo.setBookPhone(placeAttachList.get(0).getBookingConsultPhone());
        }
        respPlacePrintInfo.setPlaceAddress(tbBizPlace.getAddress());

        if (placePrintVo.getPlacePrivateOrderId() == null){
            //包场不返回订单号
            respPlacePrintInfo.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
        }

        respPlacePrintInfo.setPlaceOrderTime(DateTimeUtils.convertDate2String("yyyy-MM-dd HH:mm:ss",tbBizPlaceOrder.getOrderTime()));
        respPlacePrintInfo.setCustomerName(tbBizPlaceOrder.getCustomerName());
        //客户电话 隐藏中间四位 136****xxxx
        respPlacePrintInfo.setCustomerPhone(tbBizPlaceOrder.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        respPlacePrintInfo.setBusinessType(businessType);

        //项目类别
        if (PlaceConstants.ORDER_USE_GOODS != placePrintVo.getBusinessType()
                && PlaceConstants.ORDER_USE_OPEN_CARD != placePrintVo.getBusinessType()
                && PlaceConstants.ORDER_USE_RECHARGE_CARD != placePrintVo.getBusinessType()
                && PlaceConstants.ORDER_USE_TICKET != placePrintVo.getBusinessType()){
            if (!org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrder.getCategoryId())){
                respPlacePrintInfo.setItemName(/*iBaseService.getCommonDataCache("cagetory"+ tbBizPlaceOrder.getCategoryId().toString())*/"场地");
            }
        }
        if (PlaceConstants.ORDER_USE_OPEN_CARD == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_RECHARGE_CARD == placePrintVo.getBusinessType()){
            respPlacePrintInfo.setItemName(cardName);
        }

        TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(tbBizPlaceOrder.getCreateUid());
        if (tbAdminUser != null){
            respPlacePrintInfo.setCashier(tbAdminUser.getTrueName());
        }
        TbBizPlacePrintExample tbBizPlacePrintExample = new TbBizPlacePrintExample();
        tbBizPlacePrintExample.createCriteria().andPlaceIdEqualTo(tbBizPlace.getId());
        List<TbBizPlacePrint> tbBizPlacePrints = tbBizPlacePrintMapper.selectByExample(tbBizPlacePrintExample);
        if (CollectionUtils.isNotEmpty(tbBizPlacePrints)){
            respPlacePrintInfo.setMemo(tbBizPlacePrints.get(0).getPrintMemo());
        }

        //会员卡号，优惠金额
        if (placePrintVo.getIsMember() == PlaceConstants.YES.byteValue()
                || PlaceConstants.ORDER_USE_OPEN_CARD == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_RECHARGE_CARD == placePrintVo.getBusinessType()){
            TbBizPlaceMemberOpHistoryExample tbBizPlaceMemberOpHistoryExample = new TbBizPlaceMemberOpHistoryExample();
            tbBizPlaceMemberOpHistoryExample.createCriteria().andOrderNoEqualTo(placePrintVo.getPlaceOrderId()!=null?placePrintVo.getPlaceOrderId():placePrintVo.getPlacePrivateOrderId());
            List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList = tbBizPlaceMemberOpHistoryMapper.selectByExample(tbBizPlaceMemberOpHistoryExample);
            if (CollectionUtils.isNotEmpty(tbBizPlaceMemberOpHistoryList)){
                cardNo = tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo();
                respPlacePrintInfo.setMemberCardNo(cardNo);
                balanceAmount = tbBizPlaceMemberOpHistoryList.get(0).getOpAfterAmount().add(tbBizPlaceMemberOpHistoryList.get(0).getOpAfterGiveAmount());
                giveAmount = tbBizPlaceMemberOpHistoryList.get(0).getOpAfterGiveAmount().subtract(tbBizPlaceMemberOpHistoryList.get(0).getOpBeforeGiveAmount());
                opNum = tbBizPlaceMemberOpHistoryList.get(0).getOpCardCount();
                balanceNum = tbBizPlaceMemberOpHistoryList.get(0).getOpAfterNumbers();

                Integer cardTypeId = null;
                if (tbBizPlaceMemberOpHistoryList.get(0).getCardTypeId() != null){
                    cardTypeId = tbBizPlaceMemberOpHistoryList.get(0).getCardTypeId();
                } else {
                    TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
                    tbBizPlaceMemberCardExample.createCriteria().andInnerCardNoEqualTo(tbBizPlaceMemberOpHistoryList.get(0).getInnerCardNo());
                    List<TbBizPlaceMemberCard> tbBizPlaceMemberCardList = tbBizPlaceMemberCardMapper.selectByExample(tbBizPlaceMemberCardExample);
                    if (CollectionUtils.isEmpty(tbBizPlaceMemberCardList)){
                        try {
                            throw new Exception("根据历史记录找不到会员卡");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    cardTypeId = tbBizPlaceMemberCardList.get(0).getCardTypeId();
                }
                if (cardTypeId != null){
                    TbBizPlaceMemberCardTypeExample tbBizPlaceMemberCardTypeExample = new TbBizPlaceMemberCardTypeExample();
                    tbBizPlaceMemberCardTypeExample.createCriteria().andIdEqualTo(cardTypeId);
                    List<TbBizPlaceMemberCardType> tbBizPlaceMemberCardTypeList = tbBizPlaceMemberCardTypeMapper.selectByExample(tbBizPlaceMemberCardTypeExample);
                    if (CollectionUtils.isEmpty(tbBizPlaceMemberCardTypeList)){
                        try {
                            throw new Exception("找不到会员卡类型");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    giveNum = tbBizPlaceMemberCardTypeList.get(0).getFrequency();
                    //次卡情况
                    if (tbBizPlaceMemberCardTypeList.get(0).getType().equals(PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER)){
                        respPlacePrintInfo.setBalanceNum(balanceNum);
                        isNumCard = true;
                    }
                }
            }

            //会员卡余额
            if (balanceAmount != null){
                if (balanceAmount.compareTo(new BigDecimal(0)) != 0){
                    respPlacePrintInfo.setBalanceAmount(balanceAmount.toString()+"元");
                }
            }
        }

        //优惠价
        //if (!isNumCard){
            if (discountAmount != null){
                respPlacePrintInfo.setDiscountAmount(discountAmount);
            }else{
                discountAmount = this.getOrderDiscountAmount(tbBizPlaceOrder,realAmount);
                if (!isNotPay){
                    respPlacePrintInfo.setDiscountAmount(discountAmount);
                }
            }
        //}

        //手动调价
        if (!isNumCard){
            if (handAdjustAmount != null && handAdjustAmount.compareTo(new BigDecimal(0)) != 0){
                respPlacePrintInfo.setHandAdjustAmount(handAdjustAmount);
            }
        }

        /**
         * 实付金额旁边需显示支付方式名称；但当支付方式以下几种时，仅显示金额不显示支付方式：
         1）与场馆老板结算（老会员）
         2）与场馆老板结算（散客）
         3）与外部公司结算
         4）与公司内部核算
         */
        //实付金额 + (支付方式)
        if (realAmount != null){
            if (!isNotPay){
                if (payType != null){
                    if (isNumCard){
                        respPlacePrintInfo.setRealAmount(opNum+ "次 ("+payTypeMap.get(payType)+")");
                    }else{
                        respPlacePrintInfo.setRealAmount(realAmount.toString()+ "元 ("+payTypeMap.get(payType)+")");
                    }
                } else {
                    if (isNumCard){
                        respPlacePrintInfo.setRealAmount(opNum+"次");
                    }else{
                        respPlacePrintInfo.setRealAmount(realAmount.toString()+"元");
                    }
                }
            }
        }


        /*//订单类型  0：开单 1：包场 2：门票 3：app开单 4：商品 5:开卡 6:充值
        public static final byte ORDER_USE_BILLING = 0;
        public static final byte ORDER_USE_BLOCK_BOOKING = 1;
        public static final byte ORDER_USE_TICKET = 2;
        public static final byte ORDER_USE_APP_BILLING = 3;
        public static final byte ORDER_USE_GOODS = 4;

        private List<RespPlaceGoodsUnitInfo> respPlaceGoodsUnitInfoList;//开单、包场返回
        private List<RespPlaceGoodsTicketInfo> respPlaceGoodsTicketInfoList;//门票、小商品返回
        private List<RespPlaceGoodsCardInfo> respPlaceGoodsCardInfoList;//开卡、充值返回
        */
        if (tbBizPlaceOrder.getUseType() != null){
            List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = null;
            if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
                    && placePrintVo.getBusinessType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
                    && !StringUtils.isEmpty(placePrintVo.getPlacePrivateOrderId())){
                TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
                tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdIn(bookAllSonOrderNo);
                tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
            }else{
                TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
                tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdEqualTo(placePrintVo.getPlaceOrderId());
                tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);
            }

            //开单包场
            if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BILLING)
                    || tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)
                    || tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_APP_BILLING)){

                if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItemList)){
                    List<String> saleIdList = new ArrayList<>();
                    BigDecimal totalSalePrice = new BigDecimal(0);
                    for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
                        saleIdList.add(tbBizPlaceOrderItem.getSaleId());
                        totalSalePrice = totalSalePrice.add(tbBizPlaceOrderItem.getSalePrice());
                    }
                    TbBizPlaceUnitSalePlanExample tbBizPlaceUnitSalePlanExample = new TbBizPlaceUnitSalePlanExample();
                    tbBizPlaceUnitSalePlanExample.createCriteria().andSaleIdIn(saleIdList);
                    tbBizPlaceUnitSalePlanExample.setOrderByClause("start_time asc");
                    List<TbBizPlaceUnitSalePlan> tbBizPlaceUnitSalePlanList = tbBizPlaceUnitSalePlanMapper.selectByExample(tbBizPlaceUnitSalePlanExample);
                    if (CollectionUtils.isNotEmpty(tbBizPlaceUnitSalePlanList)){
                        respPlacePrintInfo.setCycleDate(this.getBookAllCycleDate(tbBizPlaceUnitSalePlanList));
                        List<RespPlaceGoodsUnitInfo> respPlaceGoodsUnitInfoList = new ArrayList<>();
                        Map<String, BigDecimal> unitMap = new LinkedHashMap<>();
                        StringBuilder key = null;
                        BigDecimal notMemberPrice = new BigDecimal(0);
                        BigDecimal memberPrice = new BigDecimal(0);
                        for (TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan : tbBizPlaceUnitSalePlanList){
                            key = new StringBuilder();

                            key.append(tbBizPlaceUnitSalePlan.getPlaceUnitId().toString()).append(",")
                                    .append(tbBizPlaceUnitSalePlan.getPlaceUnitName()).append(",")
                                    .append(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceUnitSalePlan.getStartTime())+"-" +
                                            DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceUnitSalePlan.getEndTime())).append(",");
                            if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
                                key.append(this.getDayOfWeek(tbBizPlaceUnitSalePlan.getDate()));
                            }else {
                                key.append(DateTimeUtils.convertDate2String("yyyy-MM-dd",tbBizPlaceUnitSalePlan.getDate()));
                            }
                            if (unitMap.containsKey(key)){
                                BigDecimal unitPrice = unitMap.get(key);
                                unitMap.put(key.toString(), unitPrice.add(tbBizPlaceUnitSalePlan.getPrice()));
                            }else{
                                unitMap.put(key.toString(), tbBizPlaceUnitSalePlan.getPrice());
                            }

                            /*RespPlaceGoodsUnitInfo respPlaceGoodsUnitInfo = new RespPlaceGoodsUnitInfo();
                        *//*private String unitDate;
                        private String unitName;
                        private String unitTime;
                        private BigDecimal unitPrice;*//*
                            respPlaceGoodsUnitInfo.setUnitName(tbBizPlaceUnitSalePlan.getPlaceUnitName());
                            respPlaceGoodsUnitInfo.setUnitTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceUnitSalePlan.getStartTime())+"-" +
                                    DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceUnitSalePlan.getEndTime()));
                            if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
                                respPlaceGoodsUnitInfo.setUnitDate(this.getDayOfWeek(tbBizPlaceUnitSalePlan.getDate()));
                                respPlaceGoodsUnitInfo.setUnitPrice(tbBizPlaceUnitSalePlan.getMemberPrice());
                            } else {
                                respPlaceGoodsUnitInfo.setUnitDate(DateTimeUtils.convertDate2String("MM-dd",tbBizPlaceUnitSalePlan.getDate()));
                                respPlaceGoodsUnitInfo.setUnitPrice(tbBizPlaceUnitSalePlan.getPrice());
                            }
                            respPlaceGoodsUnitInfoList.add(respPlaceGoodsUnitInfo);*/
                            if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
                                notMemberPrice = notMemberPrice.add(tbBizPlaceUnitSalePlan.getPrice());
                                memberPrice = memberPrice.add(tbBizPlaceUnitSalePlan.getMemberPrice());
                            }
                            //费用合计
                            totalPrice = totalPrice.add(tbBizPlaceUnitSalePlan.getPrice());
                        }
                        discountAmount = memberPrice.subtract(notMemberPrice);
                        if (MapUtils.isNotEmpty(unitMap)){
                            RespPlaceGoodsUnitInfo respPlaceGoodsUnitInfo = null;
                            for (Map.Entry<String, BigDecimal> map : unitMap.entrySet()){
                                respPlaceGoodsUnitInfo = new RespPlaceGoodsUnitInfo();
                                String[] arr = org.apache.commons.lang.StringUtils.split(map.getKey(), ",");
                                respPlaceGoodsUnitInfo.setUnitName(arr[1]);
                                respPlaceGoodsUnitInfo.setUnitTime(arr[2]);
                                respPlaceGoodsUnitInfo.setUnitDate(arr[3]);
                                respPlaceGoodsUnitInfo.setUnitPrice(map.getValue());
                                respPlaceGoodsUnitInfo.setCategoryName(iBaseService.getCommonDataCache("cagetory"+ tbBizPlaceOrder.getCategoryId().toString()));

                                respPlaceGoodsUnitInfoList.add(respPlaceGoodsUnitInfo);
                            }
                        }
                        if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
                            respPlacePrintInfo.setDiscountAmount(discountAmount);
                        }
                        /****  屏蔽包场调价和优惠——start ****/
                        if (null != placePrintVo.getIsSonOrder() && PlaceConstants.YES.equals(placePrintVo.getIsSonOrder())){
                            respPlacePrintInfo.setDiscountAmount(null);
                            respPlacePrintInfo.setHandAdjustAmount(null);
                        }
                        if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
                            respPlacePrintInfo.setDiscountAmount(null);
                            respPlacePrintInfo.setHandAdjustAmount(null);
                        }
                        /****  屏蔽包场调价和优惠——end ****/
                        respPlacePrintInfo.setRespPlaceGoodsUnitInfoList(respPlaceGoodsUnitInfoList);
                    }
                }
            }
            //门票,小商品
            if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)
                    || tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){

                if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItemList)){
                    List<RespPlaceGoodsTicketInfo> respPlaceGoodsTicketInfoList = new ArrayList<>();
                    for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
                        RespPlaceGoodsTicketInfo respPlaceGoodsTicketInfo = new RespPlaceGoodsTicketInfo();
                        //门票
                        if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_TICKET)){
                            TbPlaceTicketSalePlanExample tbPlaceTicketSalePlanExample = new TbPlaceTicketSalePlanExample();
                            TbPlaceTicketSalePlanExample.Criteria criteria = tbPlaceTicketSalePlanExample.createCriteria();
                            if (tbBizPlaceOrderItem.getSaleId() != null){
                                criteria.andSaleNoEqualTo(tbBizPlaceOrderItem.getSaleId());
                            }
                            if (tbBizPlaceOrderItem.getUnitSalePlanId() != null){
                                criteria.andIdEqualTo(tbBizPlaceOrderItem.getUnitSalePlanId());
                            }
                            List<TbPlaceTicketSalePlan> tbPlaceTicketSalePlanList = tbPlaceTicketSalePlanMapper.selectByExample(tbPlaceTicketSalePlanExample);
                            if (CollectionUtils.isNotEmpty(tbPlaceTicketSalePlanList)){
                                for (TbPlaceTicketSalePlan tbPlaceTicketSalePlan : tbPlaceTicketSalePlanList ){
                                    respPlaceGoodsTicketInfo.setCount(tbBizPlaceOrderItem.getTicketCount().toString());
                                    respPlaceGoodsTicketInfo.setName(tbPlaceTicketSalePlan.getTicketName());
                                    if (tbBizPlaceOrderItem.getTicketCount() != null && tbBizPlaceOrderItem.getTicketCount() != 0){
                                        //要求取散客价
                                        respPlaceGoodsTicketInfo.setTotalPrice(tbPlaceTicketSalePlan.getPrice().multiply(new BigDecimal(tbBizPlaceOrderItem.getTicketCount())).toString());
                                    } else {
                                        respPlaceGoodsTicketInfo.setTotalPrice(tbPlaceTicketSalePlan.getPrice().toString());
                                    }

                                    respPlaceGoodsTicketInfo.setUnitPrice(tbPlaceTicketSalePlan.getPrice().toString());
                                }

                            }
                        }
                        //小商品
                        if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
                            TbBizPlaceGoodsExample tbBizPlaceGoodsExample = new TbBizPlaceGoodsExample();
                            tbBizPlaceGoodsExample.createCriteria().andGoodsIdEqualTo(tbBizPlaceOrderItem.getSaleId());
                            List<TbBizPlaceGoods> tbBizPlaceGoodsList = tbBizPlaceGoodsMapper.selectByExample(tbBizPlaceGoodsExample);
                            if (CollectionUtils.isNotEmpty(tbBizPlaceGoodsList)){
                                respPlaceGoodsTicketInfo.setCount(tbBizPlaceOrderItem.getTicketCount().toString());
                                respPlaceGoodsTicketInfo.setName(tbBizPlaceGoodsList.get(0).getName());
                                if (tbBizPlaceOrderItem.getTicketCount() != null && tbBizPlaceOrderItem.getTicketCount() != 0){
                                    //要求取散客价
                                    respPlaceGoodsTicketInfo.setTotalPrice(tbBizPlaceGoodsList.get(0).getSalePrice().multiply(new BigDecimal(tbBizPlaceOrderItem.getTicketCount())).toString());
                                } else {
                                    respPlaceGoodsTicketInfo.setTotalPrice(tbBizPlaceGoodsList.get(0).getSalePrice().toString());
                                }
                                respPlaceGoodsTicketInfo.setUnitPrice(tbBizPlaceGoodsList.get(0).getSalePrice().toString());
                                respPlaceGoodsTicketInfo.setSaleId(tbBizPlaceOrderItem.getSaleId());
                            }
                        }
                        respPlaceGoodsTicketInfoList.add(respPlaceGoodsTicketInfo);
                    }
                    //小商品分组显示
                    if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_GOODS)){
                        Map<String,RespPlaceGoodsTicketInfo> respPlaceGoodsTicketInfoMap = new LinkedHashMap<>();
                        for (RespPlaceGoodsTicketInfo respPlaceGoodsTicketInfo : respPlaceGoodsTicketInfoList){
                            if (!respPlaceGoodsTicketInfoMap.containsKey(respPlaceGoodsTicketInfo.getSaleId())){
                                respPlaceGoodsTicketInfoMap.put(respPlaceGoodsTicketInfo.getSaleId(),respPlaceGoodsTicketInfo);
                            }else{
                                RespPlaceGoodsTicketInfo respPlaceGoodsTicketInfo1 = respPlaceGoodsTicketInfoMap.get(respPlaceGoodsTicketInfo.getSaleId());
                                respPlaceGoodsTicketInfo1.setTotalPrice(new BigDecimal(respPlaceGoodsTicketInfo1.getTotalPrice()).add(new BigDecimal(respPlaceGoodsTicketInfo.getTotalPrice())).toString());
                                respPlaceGoodsTicketInfo1.setCount(Integer.parseInt(respPlaceGoodsTicketInfo1.getCount())+Integer.parseInt(respPlaceGoodsTicketInfo.getCount())+"");
                                respPlaceGoodsTicketInfoMap.put(respPlaceGoodsTicketInfo1.getSaleId(),respPlaceGoodsTicketInfo1);
                            }
                        }
                        respPlaceGoodsTicketInfoList.clear();
                        for (Map.Entry<String, RespPlaceGoodsTicketInfo> map : respPlaceGoodsTicketInfoMap.entrySet()){
                            respPlaceGoodsTicketInfoList.add(map.getValue());
                        }
                    }
                    for (RespPlaceGoodsTicketInfo respPlaceGoodsTicketInfo : respPlaceGoodsTicketInfoList){
                        //费用合计
                        totalPrice = totalPrice.add(new BigDecimal(respPlaceGoodsTicketInfo.getTotalPrice()));
                    }
                    respPlacePrintInfo.setRespPlaceGoodsTicketInfoList(respPlaceGoodsTicketInfoList);
                }
            }

            //购物车业务订单
            if (PlaceConstants.ORDER_USE_SHOPPING_CART == placePrintVo.getBusinessType()){
                if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItemList)){
                    List<RespPlaceGoodsUnitInfo> respPlaceGoodsUnitInfoList = new ArrayList<>();
                    List<RespPlaceGoodsTicketInfo> respPlaceGoodsInfoList = new ArrayList<>();
                    List<RespPlaceGoodsTicketInfo> respPlaceTicketInfoList = new ArrayList<>();

                    for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
                        if (tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_BILLING
                                || tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_BLOCK_BOOKING
                                || tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_APP_BILLING){
                            RespPlaceGoodsUnitInfo respPlaceGoodsUnitInfo = new RespPlaceGoodsUnitInfo();
                            respPlaceGoodsUnitInfo.setCategoryName(iBaseService.getCommonDataCache("cagetory"+ tbBizPlaceOrderItem.getCategoryId().toString()));
                            respPlaceGoodsUnitInfo.setUnitDate(DateTimeUtils.convertDate2String("yyyy-MM-dd",tbBizPlaceOrderItem.getConsumeTime()));
                            respPlaceGoodsUnitInfo.setUnitName(tbBizPlaceOrderItem.getGoodsName());
                            respPlaceGoodsUnitInfo.setUnitTime(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceOrderItem.getStartTime())+"-" +
                                    DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceOrderItem.getEndTime()));
                            respPlaceGoodsUnitInfo.setUnitPrice(tbBizPlaceOrderItem.getOriginPrice());
                            respPlaceGoodsUnitInfoList.add(respPlaceGoodsUnitInfo);
                        }
                        if (tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_GOODS
                                || tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_TICKET){
                            RespPlaceGoodsTicketInfo respPlaceGoodsInfo = new RespPlaceGoodsTicketInfo();
                            respPlaceGoodsInfo.setCount(tbBizPlaceOrderItem.getTotalGoodsCount().toString());
                            respPlaceGoodsInfo.setName(tbBizPlaceOrderItem.getGoodsName());
                            respPlaceGoodsInfo.setTotalPrice(tbBizPlaceOrderItem.getOriginPrice()
                                    .multiply(new BigDecimal(tbBizPlaceOrderItem.getTotalGoodsCount())).toString());
                            respPlaceGoodsInfo.setUnitPrice(tbBizPlaceOrderItem.getOriginPrice().toString());
                            respPlaceGoodsInfo.setSaleId(tbBizPlaceOrderItem.getSaleId());
                            if (tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_GOODS){
                                respPlaceGoodsInfoList.add(respPlaceGoodsInfo);
                            }
                            if (tbBizPlaceOrderItem.getUseType().byteValue() == PlaceConstants.ORDER_USE_TICKET){
                                respPlaceTicketInfoList.add(respPlaceGoodsInfo);
                            }
                        }
                        //费用合计
                        totalPrice = totalPrice.add(tbBizPlaceOrderItem.getOriginPrice()
                                .multiply(new BigDecimal(tbBizPlaceOrderItem.getTotalGoodsCount())));
                    }
                    respPlacePrintInfo.setRespPlaceGoodsUnitInfoList(respPlaceGoodsUnitInfoList);
                    respPlacePrintInfo.setRespPlaceGoodsInfoList(respPlaceGoodsInfoList);
                    respPlacePrintInfo.setRespPlaceTicketInfoList(respPlaceTicketInfoList);
                }
            }
        }

        //开卡充值
        if (PlaceConstants.ORDER_USE_OPEN_CARD == placePrintVo.getBusinessType()
                || PlaceConstants.ORDER_USE_RECHARGE_CARD == placePrintVo.getBusinessType()){
            List<RespPlaceGoodsCardInfo> respPlaceGoodsCardInfoList = new ArrayList<>();
            RespPlaceGoodsCardInfo respPlaceGoodsCardInfo1 = new RespPlaceGoodsCardInfo();
            if (PlaceConstants.ORDER_USE_OPEN_CARD == placePrintVo.getBusinessType() ){
                respPlaceGoodsCardInfo1.setOpName("会员开卡");
            }else if (PlaceConstants.ORDER_USE_RECHARGE_CARD == placePrintVo.getBusinessType()){
                respPlaceGoodsCardInfo1.setOpName("会员充值");
            }

            respPlaceGoodsCardInfo1.setOpValue(realAmount.toString()+"元");
            RespPlaceGoodsCardInfo respPlaceGoodsCardInfo2 = new RespPlaceGoodsCardInfo();
            if (isNumCard){
                respPlaceGoodsCardInfo2.setOpName("可享");
                if (giveNum != null && giveNum !=0) {
                    respPlaceGoodsCardInfo2.setOpValue(giveNum.toString()+"次");
                }
            } else {
                respPlaceGoodsCardInfo2.setOpName("赠送");
                if (giveAmount != null && giveAmount.compareTo(new BigDecimal(0))!=0) {
                    respPlaceGoodsCardInfo2.setOpValue(giveAmount.toString()+"元");
                }
            }
            //费用合计
            totalPrice = totalPrice.add(realAmount);
            respPlacePrintInfo.setRealAmount(realAmount.toString()+"元 "+"("+payTypeMap.get(payType)+")");
            respPlaceGoodsCardInfoList.add(respPlaceGoodsCardInfo1);
            respPlaceGoodsCardInfoList.add(respPlaceGoodsCardInfo2);
            respPlacePrintInfo.setRespPlaceGoodsCardInfoList(respPlaceGoodsCardInfoList);
        }
        respPlacePrintInfo.setTotalAmount(totalPrice);
        return respPlacePrintInfo;
    }

    /**
     * 点击打印小票
     *
     * @param placeOrderId
     * @return
     */
    @Override
    public RespPlacePrintInfo printOrderInfo(String placeOrderId) {
        PlacePrintVo placePrintVo = new PlacePrintVo();
        boolean isMember = false;
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        tbBizPlaceOrderExample.createCriteria().andPlaceOrderIdEqualTo(placeOrderId);
        List<TbBizPlaceOrder> orderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        if (CollectionUtils.isEmpty(orderList)){
            placePrintVo = this.cardOrderPrintInfo(placeOrderId);
        }else {
            if (this.cardOrderPrintInfo(placeOrderId) != null){
                isMember = true;
            }
            if (isMember){
                placePrintVo.setIsMember(PlaceConstants.YES);
            }else {
                placePrintVo.setIsMember(PlaceConstants.NO);
            }
            if (orderList.get(0).getUseType() == PlaceConstants.ORDER_USE_BLOCK_BOOKING){
                placePrintVo.setBusinessType(PlaceConstants.ORDER_USE_BILLING);
                placePrintVo.setIsSonOrder(PlaceConstants.YES);
            }else {
                placePrintVo.setBusinessType(orderList.get(0).getUseType());
            }
            placePrintVo.setPlaceOrderId(placeOrderId);
        }

        return this.getPrintOrderInfo(placePrintVo);
    }

    
    
	@Override
	public RespPlacePrintInfo printBookAllOrderInfo(String placeOrderId) {

		TbBizPlace tbBizPlace = null;
        List<TbBizPlacePayItem> tbBizPlacePayItemAll = null;

        TbBizPlaceOrder tbBizPlaceOrder = new TbBizPlaceOrder();
        List<String> bookAllSonOrderNo = new ArrayList<>();
      
        //实付金额
        BigDecimal realAmount = new BigDecimal(0);
        //手动调价
      BigDecimal handAdjustAmount = new BigDecimal(0);
        //优惠金额
      /*   BigDecimal discountAmount =  new BigDecimal(0);
*/
        //费用合计
        BigDecimal totalPrice = new BigDecimal(0);

        RespPlacePrintInfo respPlacePrintInfo = new RespPlacePrintInfo();

    
        TbBizPlacePrivateOrderExample tbBizPlacePrivateOrderExample = new TbBizPlacePrivateOrderExample();
        tbBizPlacePrivateOrderExample.createCriteria().andPlaceOrderIdEqualTo(placeOrderId);
        List<TbBizPlacePrivateOrder> tbBizPlacePrivateOrderList = tbBizPlacePrivateOrderMapper.selectByExample(tbBizPlacePrivateOrderExample);
        if (CollectionUtils.isEmpty(tbBizPlacePrivateOrderList)){
            try {
                throw new Exception("不存在该包场订单");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            BeanUtils.copyProperties(tbBizPlaceOrder,tbBizPlacePrivateOrderList.get(0));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        tbBizPlaceOrder.setUseType(PlaceConstants.ORDER_USE_BLOCK_BOOKING);
        //查询所有包场子订单
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        tbBizPlaceOrderExample.createCriteria().andParentIdEqualTo(tbBizPlacePrivateOrderList.get(0).getId());
        List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        if (CollectionUtils.isEmpty(tbBizPlaceOrderList)){
            try {
                throw new Exception("不存在该包场子订单");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (TbBizPlaceOrder tbBizPlaceOrder1 : tbBizPlaceOrderList){
            bookAllSonOrderNo.add(tbBizPlaceOrder1.getPlaceOrderId());
        }
        
        TbBizPlacePayItemExample tbBizPlacePayItemExample = new TbBizPlacePayItemExample();
        tbBizPlacePayItemExample.createCriteria().andPlaceOrderIdIn(bookAllSonOrderNo);
        tbBizPlacePayItemAll = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
        tbBizPlace = tbBizPlaceMapper.selectByPrimaryKey(tbBizPlaceOrder.getPlaceId());
        handAdjustAmount = tbBizPlaceOrder.getHandAdjustAmount();
        for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemAll){
            realAmount = realAmount.add(tbBizPlacePayItem.getAmount());
        }
        respPlacePrintInfo.setOrderMemo(tbBizPlaceOrder.getDescrible());
        respPlacePrintInfo.setRealAmount(realAmount.toString());
        
        TbBizPlaceAttachExample tbBizPlaceAttachExample = new TbBizPlaceAttachExample();
        tbBizPlaceAttachExample.createCriteria().andPlaceIdEqualTo(tbBizPlaceOrder.getPlaceId());
        List<TbBizPlaceAttach> placeAttachList = tbBizPlaceAttachMapper.selectByExample(tbBizPlaceAttachExample);

        respPlacePrintInfo.setPlaceName(tbBizPlace.getName());
        if (CollectionUtils.isNotEmpty(placeAttachList)){
            respPlacePrintInfo.setBusinessTime(placeAttachList.get(0).getBusinessHourStart()+"-"+placeAttachList.get(0).getBusinessHourEnd());
            respPlacePrintInfo.setBookPhone(placeAttachList.get(0).getBookingConsultPhone());
        }
        respPlacePrintInfo.setPlaceAddress(tbBizPlace.getAddress());
       
        respPlacePrintInfo.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
        respPlacePrintInfo.setPlaceOrderTime(DateTimeUtils.convertDate2String("yyyy-MM-dd HH:mm:ss",tbBizPlaceOrder.getOrderTime()));
        respPlacePrintInfo.setCustomerName(tbBizPlaceOrder.getCustomerName());
        //客户电话 隐藏中间四位 136****xxxx
        respPlacePrintInfo.setCustomerPhone(tbBizPlaceOrder.getMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        respPlacePrintInfo.setBusinessType((int)tbBizPlaceOrder.getUseType());

        //项目类别
        respPlacePrintInfo.setItemName("场地");
        TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(tbBizPlaceOrder.getCreateUid());
        if (tbAdminUser != null){
            respPlacePrintInfo.setCashier(tbAdminUser.getTrueName());
        }
        TbBizPlacePrintExample tbBizPlacePrintExample = new TbBizPlacePrintExample();
        tbBizPlacePrintExample.createCriteria().andPlaceIdEqualTo(tbBizPlace.getId());
        List<TbBizPlacePrint> tbBizPlacePrints = tbBizPlacePrintMapper.selectByExample(tbBizPlacePrintExample);
        if (CollectionUtils.isNotEmpty(tbBizPlacePrints)){
            respPlacePrintInfo.setMemo(tbBizPlacePrints.get(0).getPrintMemo());
        }

        
        List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList = null;
        TbBizPlaceOrderItemExample tbBizPlaceOrderItemExample = new TbBizPlaceOrderItemExample();
        tbBizPlaceOrderItemExample.createCriteria().andPlaceOrderIdIn(bookAllSonOrderNo);
        tbBizPlaceOrderItemList = tbBizPlaceOrderItemMapper.selectByExample(tbBizPlaceOrderItemExample);

        if (CollectionUtils.isNotEmpty(tbBizPlaceOrderItemList)){
            List<String> saleIdList = new ArrayList<>();
//            BigDecimal totalSalePrice = new BigDecimal(0);
            for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
                saleIdList.add(tbBizPlaceOrderItem.getSaleId());
//                totalSalePrice = totalSalePrice.add(tbBizPlaceOrderItem.getSalePrice());
            }
            TbBizPlaceUnitSalePlanExample tbBizPlaceUnitSalePlanExample = new TbBizPlaceUnitSalePlanExample();
            tbBizPlaceUnitSalePlanExample.createCriteria().andSaleIdIn(saleIdList);
            tbBizPlaceUnitSalePlanExample.setOrderByClause("start_time asc");
            List<TbBizPlaceUnitSalePlan> tbBizPlaceUnitSalePlanList = tbBizPlaceUnitSalePlanMapper.selectByExample(tbBizPlaceUnitSalePlanExample);
            if (CollectionUtils.isNotEmpty(tbBizPlaceUnitSalePlanList)){
                respPlacePrintInfo.setCycleDate(this.getBookAllCycleDate(tbBizPlaceUnitSalePlanList));
                List<RespPlaceGoodsUnitInfo> respPlaceGoodsUnitInfoList = new ArrayList<>();
                Map<String, BigDecimal> unitMap = new LinkedHashMap<>();
                StringBuilder key = null;
                BigDecimal notMemberPrice = new BigDecimal(0);
                for (TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan : tbBizPlaceUnitSalePlanList){
                    key = new StringBuilder();

                    key.append(tbBizPlaceUnitSalePlan.getPlaceUnitId().toString()).append(",")
                            .append(tbBizPlaceUnitSalePlan.getPlaceUnitName()).append(",")
                            .append(DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceUnitSalePlan.getStartTime())+"-" +
                                    DateTimeUtils.convertDate2String("HH:mm",tbBizPlaceUnitSalePlan.getEndTime())).append(",");
                    if (tbBizPlaceOrder.getUseType().equals(PlaceConstants.ORDER_USE_BLOCK_BOOKING)){
                        key.append(this.getDayOfWeek(tbBizPlaceUnitSalePlan.getDate()));
                    }else {
                        key.append(DateTimeUtils.convertDate2String("yyyy-MM-dd",tbBizPlaceUnitSalePlan.getDate()));
                    }
                    if (unitMap.containsKey(key)){
                        BigDecimal unitPrice = unitMap.get(key);
                        unitMap.put(key.toString(), unitPrice.add(tbBizPlaceUnitSalePlan.getPrice()));
                    }else{
                        unitMap.put(key.toString(), tbBizPlaceUnitSalePlan.getPrice());
                    }

                    notMemberPrice = notMemberPrice.add(tbBizPlaceUnitSalePlan.getPrice());
                    //费用合计
                    totalPrice = totalPrice.add(tbBizPlaceUnitSalePlan.getPrice());
                }
//                discountAmount = memberPrice.subtract(notMemberPrice);
                if (MapUtils.isNotEmpty(unitMap)){
                    RespPlaceGoodsUnitInfo respPlaceGoodsUnitInfo = null;
                    for (Map.Entry<String, BigDecimal> map : unitMap.entrySet()){
                        respPlaceGoodsUnitInfo = new RespPlaceGoodsUnitInfo();
                        String[] arr = org.apache.commons.lang.StringUtils.split(map.getKey(), ",");
                        respPlaceGoodsUnitInfo.setUnitName(arr[1]);
                        respPlaceGoodsUnitInfo.setUnitTime(arr[2]);
                        respPlaceGoodsUnitInfo.setUnitDate(arr[3]);
                        respPlaceGoodsUnitInfo.setUnitPrice(map.getValue());
                        respPlaceGoodsUnitInfo.setCategoryName(iBaseService.getCommonDataCache("cagetory"+ tbBizPlaceOrder.getCategoryId().toString()));

                        respPlaceGoodsUnitInfoList.add(respPlaceGoodsUnitInfo);
                    }
                }
                respPlacePrintInfo.setDiscountAmount(null);
                respPlacePrintInfo.setHandAdjustAmount(handAdjustAmount);
                respPlacePrintInfo.setRespPlaceGoodsUnitInfoList(respPlaceGoodsUnitInfoList);
            }
        }
    
        respPlacePrintInfo.setTotalAmount(totalPrice);
        return respPlacePrintInfo;
    
	}
    /**
     * 会员交易流水打印小票
     * @param placeOrderId
     * @return
     */
    private PlacePrintVo cardOrderPrintInfo(String placeOrderId){
        TbBizPlaceMemberOpHistoryExample tbBizPlaceMemberOpHistoryExample = new TbBizPlaceMemberOpHistoryExample();
        tbBizPlaceMemberOpHistoryExample.createCriteria().andOrderNoEqualTo(placeOrderId);
        List<TbBizPlaceMemberOpHistory> tbBizPlaceMemberOpHistoryList = tbBizPlaceMemberOpHistoryMapper.selectByExample(tbBizPlaceMemberOpHistoryExample);
        if (CollectionUtils.isEmpty(tbBizPlaceMemberOpHistoryList)){
            return null;
        }
        //可能是退款订单
        if (tbBizPlaceMemberOpHistoryList.get(0).getSrc().equals(PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_REFUND)){
            TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
            tbBizPlaceOrderExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceMemberOpHistoryList.get(0).getRefundNo());
            List<TbBizPlaceOrder> orderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
            if (CollectionUtils.isNotEmpty(orderList)){
                PlacePrintVo placePrintVo = new PlacePrintVo();
                placePrintVo.setIsMember(PlaceConstants.YES);
                placePrintVo.setBusinessType(orderList.get(0).getUseType());
                placePrintVo.setPlaceOrderId(orderList.get(0).getPlaceOrderId());
                return placePrintVo;
            }
        }
        PlacePrintVo placePrintVo = new PlacePrintVo();
        placePrintVo.setIsMember(PlaceConstants.YES);
        if (PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_OPEN.equals(tbBizPlaceMemberOpHistoryList.get(0).getSrc())){
            //开卡
            placePrintVo.setBusinessType(PlaceConstants.ORDER_USE_OPEN_CARD);
        } else if (PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_RECHARGE.equals(tbBizPlaceMemberOpHistoryList.get(0).getSrc())){
            //充值
            placePrintVo.setBusinessType(PlaceConstants.ORDER_USE_RECHARGE_CARD);
        }

        placePrintVo.setPlaceOrderId(placeOrderId);
        return placePrintVo;
    }

    /**
     * 优惠金额计算
     * @param tbBizPlaceOrder
     * @param realAmount
     * @return
     */
    private BigDecimal getOrderDiscountAmount(TbBizPlaceOrder tbBizPlaceOrder,BigDecimal realAmount){
        BigDecimal discountAmount = null;
        /*if (tbBizPlaceOrder == null || realAmount == null){
            //app 优惠券全部抵扣
            TbBizPlacePayItemExample tbBizPlacePayItemExample =  new TbBizPlacePayItemExample();
            tbBizPlacePayItemExample.createCriteria().andPlaceOrderIdEqualTo(tbBizPlaceOrder.getPlaceOrderId());
            List<TbBizPlacePayItem> tbBizPlacePayItemList = tbBizPlacePayItemMapper.selectByExample(tbBizPlacePayItemExample);
            if (CollectionUtils.isNotEmpty(tbBizPlacePayItemList)){
                //优惠的支付方式
                List<String> discountPayTypeList = Arrays.asList("7", "11");
                for (TbBizPlacePayItem tbBizPlacePayItem : tbBizPlacePayItemList){
                    if (discountPayTypeList.contains(tbBizPlacePayItem.getPayType().toString())){
                        discountAmount = (discountAmount == null ?new BigDecimal(0) : discountAmount).subtract(tbBizPlacePayItem.getAmount());
                    }
                }
                return discountAmount;
            }
            return null;
        }*/

        //线下订单优惠金额计算  应付金额-手动调价-实付金额
        discountAmount = new BigDecimal(0).subtract(tbBizPlaceOrder.getDebit()
                .add(tbBizPlaceOrder.getHandAdjustAmount()==null?new BigDecimal(0):tbBizPlaceOrder.getHandAdjustAmount())
                .subtract(realAmount));

        if (discountAmount != null){
            if (discountAmount.compareTo(new BigDecimal(0)) == 0){
                discountAmount = null;
            }
        }
        return discountAmount;
    }

	/**
	 * 获取pos条形码信息
	 */
	@Override
	public RespBarCodeInfo getPosUnionPayInfo(String placeOrderId) {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(placeOrderId)){
			return null;
		}
		return posUnionPayMapper.queryBarCodeInfo(placeOrderId);
	}

    /** 订单明细转换订单流水
     * @param tbBizPlaceOrder
     * @param businessType //交易类型：0：支付 1：退款
     * @param refundType //0：全额 1：部分 退款
     * @return
     */
    @Override
    public List<ReqOrderHistory> tbBizPlaceOrderItem2ReqOrderHistory(TbBizPlaceOrder tbBizPlaceOrder,
                                                               List<TbBizPlaceOrderItem> tbBizPlaceOrderItemList,
                                                               byte businessType,
                                                               byte refundType, TbBizPlaceMemberCard tbBizPlaceMemberCard) {
        if (tbBizPlaceOrder == null){
            return null;
        }
        if (CollectionUtils.isEmpty(tbBizPlaceOrderItemList)){
            return null;
        }
        List<ReqOrderHistory> reqOrderHistoryList = new ArrayList<>();
        for (TbBizPlaceOrderItem tbBizPlaceOrderItem : tbBizPlaceOrderItemList){
            ReqOrderHistory reqOrderHistory = new ReqOrderHistory();
            reqOrderHistory.setBusinessType(businessType);
            //赠送商品
            if (!org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrderItem.getSalePrice())
                    && !org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrderItem.getIsGive())){
                if (tbBizPlaceOrderItem.getSalePrice().compareTo(BigDecimal.ZERO) == 0
                        && PlaceConstants.YES.equals(tbBizPlaceOrderItem.getIsGive())){
                    //2 ：支付流水业务赠送
                    reqOrderHistory.setBusinessType((byte)2);
                    if (businessType == (byte) 1){
                        continue;//赠送不退
                    }
                }
            }

            reqOrderHistory.setPlaceId(tbBizPlaceOrder.getPlaceId());
            if (!org.springframework.util.StringUtils.isEmpty(tbBizPlaceMemberCard)){
                //次卡不存在退金额
                if (!org.springframework.util.StringUtils.isEmpty(tbBizPlaceMemberCard.getCardType())
                        && tbBizPlaceMemberCard.getCardType().equals(PlaceConstants.PLACE_MEMBER_CARD_TYPE_NUMBER)){
                    reqOrderHistory.setAmount(BigDecimal.ZERO);
                }else {
                    reqOrderHistory.setAmount(tbBizPlaceOrderItem.getSalePrice());
                }
            }else {
                reqOrderHistory.setAmount(tbBizPlaceOrderItem.getSalePrice());
            }
            reqOrderHistory.setParentId(tbBizPlaceOrder.getParentId());
            reqOrderHistory.setPlatform(tbBizPlaceOrder.getPlatform());
            reqOrderHistory.setRefundType(refundType);
            reqOrderHistory.setSourceType(tbBizPlaceOrder.getSourceType());
            reqOrderHistory.setUid(tbBizPlaceOrder.getUid());
            reqOrderHistory.setUseType(tbBizPlaceOrderItem.getUseType());
            reqOrderHistory.setPlaceOrderId(tbBizPlaceOrder.getPlaceOrderId());
            reqOrderHistory.setOrderItemUuid(tbBizPlaceOrderItem.getItemUuid());
            if (businessType == (byte) 0){//支付
                reqOrderHistory.setTicketCount(tbBizPlaceOrderItem.getTicketCount());
                reqOrderHistory.setGoodsCount(tbBizPlaceOrderItem.getTotalGoodsCount());
            }else if (businessType == (byte) 1){//退款
                reqOrderHistory.setTicketCount(tbBizPlaceOrderItem.getTicketCount());
                reqOrderHistory.setGoodsCount(tbBizPlaceOrderItem.getTotalGoodsCount()-tbBizPlaceOrderItem.getSurplusGoodsCount());
            }
            reqOrderHistoryList.add(reqOrderHistory);
        }

        return reqOrderHistoryList;
    }

    /**
     * 对公转账订单转换审核记录
     * @param tbBizPlaceOrder
     *  业务类型 1：开单 2.包场 3.门票 4.小商品 5：购物车 6.开卡 7.充值 8.会员导入
     * @return
     */
    @Override
    public PlaceFinanceAuditVo tbBizPlaceOrder2PlaceFinanceAuditVo(TbBizPlaceOrder tbBizPlaceOrder) {
        PlaceFinanceAuditVo placeFinanceAuditVo = new PlaceFinanceAuditVo();

        if (org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrder.getUseType())){
            throw new BizException("订单业务类型异常");
        }
        Byte subType = null;
        //包场单个支付情况按开单处理
        if (PlaceConstants.ORDER_USE_BILLING == tbBizPlaceOrder.getUseType().byteValue()
                || (PlaceConstants.ORDER_USE_BLOCK_BOOKING == tbBizPlaceOrder.getUseType().byteValue()
                && tbBizPlaceOrder.getParentId().intValue() != 0)){
            subType = 1;
        }
        if (PlaceConstants.ORDER_USE_BLOCK_BOOKING == tbBizPlaceOrder.getUseType().byteValue()
                && tbBizPlaceOrder.getParentId().intValue() == 0){
            subType = 2;
        }
        if (PlaceConstants.ORDER_USE_TICKET == tbBizPlaceOrder.getUseType().byteValue()){
            subType = 3;
        }
        if (PlaceConstants.ORDER_USE_GOODS == tbBizPlaceOrder.getUseType().byteValue()){
            subType = 4;
        }
        if (PlaceConstants.ORDER_USE_SHOPPING_CART == tbBizPlaceOrder.getUseType().byteValue()){
            subType = 5;
        }

        if (org.springframework.util.StringUtils.isEmpty(subType)){
            throw new BizException("未知的业务类型订单");
        }
        placeFinanceAuditVo.setPlaceId(tbBizPlaceOrder.getPlaceId());
        placeFinanceAuditVo.setBusinessType(Constants.BYTE_ONE);
        placeFinanceAuditVo.setBusinessNo(tbBizPlaceOrder.getPlaceOrderId());
        placeFinanceAuditVo.setSubType(subType);
        placeFinanceAuditVo.setSubjectUid(tbBizPlaceOrder.getCreateUid());
        if (subType == (byte)2){
            placeFinanceAuditVo.setParentId(tbBizPlaceOrder.getId());
        }
        if (!org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrder.getCreateUid())){
            TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(tbBizPlaceOrder.getCreateUid());
            if (!org.springframework.util.StringUtils.isEmpty(tbAdminUser)){
                placeFinanceAuditVo.setSubjectUserName(tbAdminUser.getTrueName());
            }
        }

        return placeFinanceAuditVo;
    }

    /**
     * 对公转账会员交易记录转换审核记录
     * @param tbBizPlaceMemberOpHistory  会员交易记录
     * 业务类型 1：开单 2.包场 3.门票 4.小商品 5：购物车 6.开卡 7.充值 8.会员导入
     * @return
     */
    @Override
    public PlaceFinanceAuditVo tbBizPlaceMemberOpHistory2PlaceFinanceAuditVo(TbBizPlaceMemberOpHistory tbBizPlaceMemberOpHistory) {
        PlaceFinanceAuditVo placeFinanceAuditVo = new PlaceFinanceAuditVo();

        if (org.springframework.util.StringUtils.isEmpty(tbBizPlaceMemberOpHistory.getSrc())){
            throw new BizException("会员业务类型异常");
        }
        Byte subType = null;
        if (PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_OPEN.intValue() == tbBizPlaceMemberOpHistory.getSrc().intValue()){
            subType = 6;
        }
        if (PlaceConstants.PLACE_MEMBER_CARD_OP_HISTORY_SRC_RECHARGE.intValue() == tbBizPlaceMemberOpHistory.getSrc().intValue()){
            subType = 7;
        }
        if (org.springframework.util.StringUtils.isEmpty(subType)){
            throw new BizException("未知的业务类型交易记录");
        }

        placeFinanceAuditVo.setPlaceId(tbBizPlaceMemberOpHistory.getPlaceId());
        placeFinanceAuditVo.setBusinessType(Constants.BYTE_TWO);
        placeFinanceAuditVo.setBusinessNo(tbBizPlaceMemberOpHistory.getOrderNo());
        placeFinanceAuditVo.setSubType(subType);
        placeFinanceAuditVo.setMemberCardNo(tbBizPlaceMemberOpHistory.getInnerCardNo());//APP开卡 会员卡号为null
        placeFinanceAuditVo.setSubjectUid(tbBizPlaceMemberOpHistory.getCreateUid());
        if (!org.springframework.util.StringUtils.isEmpty(tbBizPlaceMemberOpHistory.getCreateUid())){
            TbAdminUser tbAdminUser = tbAdminUserMapper.selectByPrimaryKey(tbBizPlaceMemberOpHistory.getCreateUid());
            if (!org.springframework.util.StringUtils.isEmpty(tbAdminUser)){
                placeFinanceAuditVo.setSubjectUserName(tbAdminUser.getTrueName());
            }
        }
        return placeFinanceAuditVo;
    }

    /**
     * 商家端销售计划状态异常情况释放
     * @param releaseSalePlanList
     */
    @Async
    @Override
    public void releaseSalePlanStatus(List<TbBizPlaceUnitSalePlan> releaseSalePlanList){
        if (CollectionUtils.isEmpty(releaseSalePlanList)){
            return;
        }
        List<String> saleIds = new ArrayList<>();
        for (TbBizPlaceUnitSalePlan bizPlaceUnitSalePlan : releaseSalePlanList){
            if (!org.springframework.util.StringUtils.isEmpty(bizPlaceUnitSalePlan.getSaleId())){
                saleIds.add(bizPlaceUnitSalePlan.getSaleId());
            }
        }
        if (CollectionUtils.isEmpty(saleIds)){
            return;
        }
        TbBizPlaceUnitSalePlanExample salePlanExample = new TbBizPlaceUnitSalePlanExample();
        salePlanExample.createCriteria().andSaleIdIn(saleIds);
        TbBizPlaceUnitSalePlan updateSalePlan = new TbBizPlaceUnitSalePlan();
        updateSalePlan.setStatus((byte)1);
        tbBizPlaceUnitSalePlanMapper.updateByExampleSelective(updateSalePlan,salePlanExample);
    }

    /**
     * 返回包场周期 根据订单的销售计划
     */
    private String getBookAllCycleDate(List<TbBizPlaceUnitSalePlan> tbBizPlaceUnitSalePlanList){
        String sDate_eDate = null;
        ComparatorChain chain = new ComparatorChain();
        chain.addComparator(new BeanComparator("date"),false);//true,fase正序反序
        Collections.sort(tbBizPlaceUnitSalePlanList,chain);
        int size = tbBizPlaceUnitSalePlanList.size();

        sDate_eDate = DateTimeUtils.convertDate2String("yyyy-MM-dd",tbBizPlaceUnitSalePlanList.get(0).getDate())
                + "至" + DateTimeUtils.convertDate2String("yyyy-MM-dd",tbBizPlaceUnitSalePlanList.get(size - 1).getDate());

        return sDate_eDate;
    }

    /**
     * 购物车消费退款发送短信提醒
     * @param reqOrderHistoryList
     */
    private void shoppingCartOrderSms(List<ReqOrderHistory> reqOrderHistoryList){
        //只针对购物车会员订单才能发送短信
        if (org.apache.commons.collections.CollectionUtils.isEmpty(reqOrderHistoryList)){
            return;
        }
        BigDecimal consumePrice = new BigDecimal(0);
        for (ReqOrderHistory reqOrderHistory : reqOrderHistoryList){
            if (!org.springframework.util.StringUtils.isEmpty(reqOrderHistory.getAmount())){
                consumePrice = consumePrice.add(reqOrderHistory.getAmount());
            }
        }

        ReqOrderHistory reqOrderHistory = reqOrderHistoryList.get(0);
        if (!org.springframework.util.StringUtils.isEmpty(reqOrderHistory.getBusinessType())){
            if (reqOrderHistory.getBusinessType().byteValue() == 1){
                //退款不支持发送短信
                return;
            }
        }
        if (org.springframework.util.StringUtils.isEmpty(reqOrderHistory)){
            return;
        }
        if (org.springframework.util.StringUtils.isEmpty(reqOrderHistory.getPlaceOrderId())){
            return;
        }
        TbBizPlaceOrderExample tbBizPlaceOrderExample = new TbBizPlaceOrderExample();
        TbBizPlaceOrderExample.Criteria tbBizPlaceOrderCriteria = tbBizPlaceOrderExample.createCriteria();
        tbBizPlaceOrderCriteria.andIsEffectiveEqualTo(PlaceConstants.YES);
        tbBizPlaceOrderCriteria.andPlaceOrderIdEqualTo(reqOrderHistory.getPlaceOrderId());
        tbBizPlaceOrderCriteria.andUseTypeEqualTo(PlaceConstants.ORDER_USE_SHOPPING_CART);
        tbBizPlaceOrderCriteria.andIsMemberEqualTo(PlaceConstants.YES);
        List<TbBizPlaceOrder> tbBizPlaceOrderList = tbBizPlaceOrderMapper.selectByExample(tbBizPlaceOrderExample);
        if (org.apache.commons.collections.CollectionUtils.isEmpty(tbBizPlaceOrderList)){
            return;
        }
        TbBizPlaceOrder tbBizPlaceOrder = tbBizPlaceOrderList.get(0);
        if (org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrder.getInnerCardNo())){
            return;
        }
        if (org.springframework.util.StringUtils.isEmpty(tbBizPlaceOrder.getMobile())){
            return;
        }
        TbBizPlaceMemberCardExample tbBizPlaceMemberCardExample = new TbBizPlaceMemberCardExample();
        TbBizPlaceMemberCardExample.Criteria tbBizPlaceMemberCardCriteria = tbBizPlaceMemberCardExample.createCriteria();
        tbBizPlaceMemberCardCriteria.andInnerCardNoEqualTo(tbBizPlaceOrder.getInnerCardNo());
        List<TbBizPlaceMemberCard> tbBizPlaceMemberCardList = tbBizPlaceMemberCardMapper.selectByExample(tbBizPlaceMemberCardExample);
        if (org.apache.commons.collections.CollectionUtils.isEmpty(tbBizPlaceMemberCardList)){
            return;
        }
        TbBizPlaceMemberCard tbBizPlaceMemberCard = tbBizPlaceMemberCardList.get(0);

        TbBizPlace place = placeBaseService.getPlaceById(tbBizPlaceOrder.getPlaceId());

        List<String> mobileList = new ArrayList<>();
        List<String> paramList = new ArrayList<>();
        //成功消费发送短信提醒
        String[] args = new String[7];
        args[0] = place != null ? place.getName() : "";
        args[1] = tbBizPlaceMemberCard.getCardName() != null ? tbBizPlaceMemberCard.getCardName() : "";
        args[2] = DateUtils.getCurrentTime();
        args[3] = "商家端购物车";
        args[4] = consumePrice.compareTo(BigDecimal.ZERO) != 0 ? consumePrice.toString() + "元" : "";
        args[5] = tbBizPlaceMemberCard != null ? tbBizPlaceMemberCard.getSurplusAmount().add(tbBizPlaceMemberCard.getSurplusGiveAmount()).toString() + "元" : "";
        // 联系电话优先取订场咨询电话，订场电话为空时，取客服电话
        TbBizPlaceAttach placeAttach = placeBaseService.getBizPlaceAttach(reqOrderHistory.getPlaceId());
        if (null != placeAttach && org.apache.commons.lang3.StringUtils.isNotEmpty(placeAttach.getBookingConsultPhone())) {
            args[6] = placeAttach.getBookingConsultPhone();
        } else {
            // 获取系统配置的百灵鸟客服热线
            TbCommonParameter parameterVo = baseService.getParameterByKey(SysParameterConfigEnum.CONFIG_APP_SERVICE_MOBILE.getCode());
            args[6] = parameterVo != null ? parameterVo.getParameterValue() : "";
        }
        mobileList.add(tbBizPlaceOrder.getMobile());
        paramList.addAll(Arrays.asList(args));
        try {
            placeSmsService.sendSms(mobileList, SmsApi.MESSAGE_TEMPLATE_PLACE_MEMBER_CONSUME_NOTICE, PlaceConstants.PLACE_SMS_TYPE_MESSAGE, paramList);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            logger.error("购物车会员订单短信提示发送失败.");
        }
    }

    /**
     * 返回周几
     *
     * @param date
     * @return
     */
    public String getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int number = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        String[] str = {"周日", "周一", "周二", "周三", "周四", "周五", "周六",};
        return new String(str[number]);
    }

}
